//
//  LocalListHelper.m
//  Final
//
//  Created by Pibinuo on 15/12/8.
//  Copyright © 2015年 皮比诺. All rights reserved.
//

#import "LocalListHelper.h"
#import "LocalLIst.h"

@implementation LocalListHelper


+(LocalListHelper *)defaultHelpers
{
    static dispatch_once_t onceToken;
    static LocalListHelper * loccallisHelper = nil;
    dispatch_once(&onceToken, ^{
        loccallisHelper = [[LocalListHelper alloc]init];
        loccallisHelper.dataDict = [NSMutableDictionary dictionary];
    });
    return loccallisHelper;
}

-(void)loadData
{
    NSURL * url= [NSURL URLWithString:@"http://api.irecommend.ifeng.com/citylist.php"];
    NSURLSession * session = [NSURLSession sharedSession];
    NSURLSessionDataTask * task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data != nil) {
            
            NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
            NSDictionary * dic = dict[@"list"];
            for (NSString * s in dic) {
                NSMutableArray * arr = [NSMutableArray array];
                for (NSDictionary * dict1 in dic[s])
                {
                    LocalLIst * model = [[LocalLIst alloc] init];
                    
                    [model setValuesForKeysWithDictionary:dict1];
                    
                    [arr addObject:model];
                }
                
                [self.dataDict setObject:arr forKey:s];
                
                
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [[NSNotificationCenter defaultCenter] postNotificationName:@"finishList" object:nil];
                
            });
            
            
        }
        
        
    }];
    
    [task resume];
}


-(NSInteger)modelCount
{
    return self.dataDict.count;
}

@end
